home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / music / opl3.zip / OPL3.C next >
C/C++ Source or Header  |  1994-12-24  |  27KB  |  1,225 lines

  1. /*
  2.  * sound/opl3.c
  3.  *
  4.  * A low level driver for Yamaha YM3812 and OPL-3 -chips
  5.  *
  6.  * Copyright by Hannu Savolainen 1993
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions are
  10.  * met: 1. Redistributions of source code must retain the above copyright
  11.  * notice, this list of conditions and the following disclaimer. 2.
  12.  * Redistributions in binary form must reproduce the above copyright notice,
  13.  * this list of conditions and the following disclaimer in the documentation
  14.  * and/or other materials provided with the distribution.
  15.  *
  16.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
  17.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19.  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  20.  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22.  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26.  * SUCH DAMAGE.
  27.  *
  28.  * $Id: opl3.c,v 1.7 1994/10/01 02:16:50 swallace Exp $
  29.  */
  30.  
  31. /*
  32.  * Major improvements to the FM handling 30AUG92 by Rob Hooft,
  33.  * hooft@chem.ruu.nl
  34.  */
  35.  
  36. #include "sound_config.h"
  37.  
  38. #if defined(CONFIGURE_SOUNDCARD) && !defined(EXCLUDE_YM3812)
  39.  
  40. #include "opl3.h"
  41.  
  42. #define MAX_VOICE    18
  43. #define OFFS_4OP    11    /*
  44.                    * * * Definitions for the operators OP3 and
  45.                    * * OP4 * * begin here   */
  46.  
  47. static int      opl3_enabled = 0;
  48. static int      left_address = 0x388, right_address = 0x388, both_address = 0;
  49.  
  50. static int      nr_voices = 9;
  51. static int      logical_voices[MAX_VOICE] =
  52. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
  53.  
  54. struct voice_info
  55.   {
  56.     unsigned char   keyon_byte;
  57.     long            bender;
  58.     long            bender_range;
  59.     unsigned long   orig_freq;
  60.     unsigned long   current_freq;
  61.     int             mode;
  62.   };
  63.  
  64. static struct voice_info voices[MAX_VOICE];
  65. static struct voice_alloc_info *voice_alloc;
  66. static struct channel_info *chn_info;
  67.  
  68. static struct sbi_instrument *instrmap;
  69. static struct sbi_instrument *active_instrument[MAX_VOICE] =
  70. {NULL};
  71.  
  72. static struct synth_info fm_info =
  73. {"OPL-2", 0, SYNTH_TYPE_FM, FM_TYPE_ADLIB, 0, 9, 0, SBFM_MAXINSTR, 0};
  74.  
  75. static int      already_initialized = 0;
  76.  
  77. static int      opl3_ok = 0;
  78. static int      opl3_busy = 0;
  79. static int      fm_model = 0;    /*
  80.  
  81.  
  82.                  * *  * * 0=no fm, 1=mono, 2=SB Pro 1, 3=SB
  83.                  * Pro 2 * *    */
  84.  
  85. static int      store_instr (int instr_no, struct sbi_instrument *instr);
  86. static void     freq_to_fnum (int freq, int *block, int *fnum);
  87. static void     opl3_command (int io_addr, unsigned int addr, unsigned int val);
  88. static int      opl3_kill_note (int dev, int voice, int note, int velocity);
  89. static unsigned char connection_mask = 0x00;
  90.  
  91. void
  92. enable_opl3_mode (int left, int right, int both)
  93. {
  94.   if (opl3_enabled)
  95.     return;
  96.  
  97.   opl3_enabled = 1;
  98.   left_address = left;
  99.   right_address = right;
  100.   both_address = both;
  101.   fm_info.capabilities = SYNTH_CAP_OPL3;
  102.   fm_info.synth_subtype = FM_TYPE_OPL3;
  103. }
  104.  
  105. static void
  106. enter_4op_mode (void)
  107. {
  108.   int             i;
  109.   static int      voices_4op[MAX_VOICE] =
  110.   {0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17};
  111.  
  112.   connection_mask = 0x3f;    /* Connect all possible 4 OP voices */
  113.   opl3_command (right_address, CONNECTION_SELECT_REGISTER, 0x3f);
  114.  
  115.   for (i = 0; i < 3; i++)
  116.     physical_voices[i].voice_mode = 4;
  117.   for (i = 3; i < 6; i++)
  118.     physical_voices[i].voice_mode = 0;
  119.  
  120.   for (i = 9; i < 12; i++)
  121.     physical_voices[i].voice_mode = 4;
  122.   for (i = 12; i < 15; i++)
  123.     physical_voices[i].voice_mode = 0;
  124.  
  125.   for (i = 0; i < 12; i++)
  126.     logical_voices[i] = voices_4op[i];
  127.   voice_alloc->max_voice = nr_voices = 12;
  128. }
  129.  
  130. static int
  131. opl3_ioctl (int dev,
  132.         unsigned int cmd, unsigned int arg)
  133. {
  134.   switch (cmd)
  135.     {
  136.  
  137.     case SNDCTL_FM_LOAD_INSTR:
  138.       {
  139.     struct sbi_instrument ins;
  140.  
  141.     IOCTL_FROM_USER ((char *) &ins, (char *) arg, 0, sizeof (ins));
  142.  
  143.     if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)
  144.       {
  145.         printk ("FM Error: Invalid instrument number %d\n", ins.channel);
  146.         return RET_ERROR (EINVAL);
  147.       }
  148.  
  149.     pmgr_inform (dev, PM_E_PATCH_LOADED, ins.channel, 0, 0, 0);
  150.     return store_instr (ins.channel, &ins);
  151.       }
  152.       break;
  153.  
  154.     case SNDCTL_SYNTH_INFO:
  155.       fm_info.nr_voices = (nr_voices == 12) ? 6 : nr_voices;
  156.  
  157.       IOCTL_TO_USER ((char *) arg, 0, &fm_info, sizeof (fm_info));
  158.       return 0;
  159.       break;
  160.  
  161.     case SNDCTL_SYNTH_MEMAVL:
  162.       return 0x7fffffff;
  163.       break;
  164.  
  165.     case SNDCTL_FM_4OP_ENABLE:
  166.       if (opl3_enabled)
  167.     enter_4op_mode ();
  168.       return 0;
  169.       break;
  170.  
  171.     default:
  172.       return RET_ERROR (EINVAL);
  173.     }
  174.  
  175. }
  176.  
  177. int
  178. opl3_detect (int ioaddr)
  179. {
  180.   /*
  181.    * This function returns 1 if the FM chicp is present at the given I/O port
  182.    * The detection algorithm plays with the timer built in the FM chip and
  183.    * looks for a change in the status register.
  184.    *
  185.    * Note! The timers of the FM chip are not connected to AdLib (and compatible)
  186.    * boards.
  187.    *
  188.    * Note2! The chip is initialized if detected.
  189.    */
  190.  
  191.   unsigned char   stat1, stat2;
  192.   int             i;
  193.  
  194.   if (already_initialized)
  195.     {
  196.       return 0;            /*
  197.                  * Do avoid duplicate initializations
  198.                  */
  199.     }
  200.  
  201.   if (opl3_enabled)
  202.     ioaddr = left_address;
  203.  
  204.   opl3_command (ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);    /*
  205.                                          * Reset
  206.                                          * timers
  207.                                          * 1
  208.                                          * and
  209.                                          * 2
  210.                                          */
  211.   opl3_command (ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);    /*
  212.                                  * Reset the
  213.                                  * IRQ of FM
  214.                                  * * chicp
  215.                                  */
  216.  
  217.   stat1 = INB (ioaddr);        /*
  218.                  * Read status register
  219.                  */
  220.  
  221.   if ((stat1 & 0xE0) != 0x00)
  222.     {
  223.       return 0;            /*
  224.                  * Should be 0x00
  225.                  */
  226.     }
  227.  
  228.   opl3_command (ioaddr, TIMER1_REGISTER, 0xff);    /*
  229.                              * Set timer 1 to
  230.                              * 0xff
  231.                              */
  232.   opl3_command (ioaddr, TIMER_CONTROL_REGISTER,
  233.         TIMER2_MASK | TIMER1_START);    /*
  234.                          * Unmask and start timer 1
  235.                          */
  236.  
  237.   /*
  238.    * Now we have to delay at least 80 msec
  239.    */
  240.  
  241.   for (i = 0; i < 50; i++)
  242.     tenmicrosec ();        /*
  243.                  * To be sure
  244.                  */
  245.  
  246.   stat2 = INB (ioaddr);        /*
  247.                  * Read status after timers have expired
  248.                  */
  249.  
  250.   /*
  251.    * Stop the timers
  252.    */
  253.  
  254.   opl3_command (ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);    /*
  255.                                          * Reset
  256.                                          * timers
  257.                                          * 1
  258.                                          * and
  259.                                          * 2
  260.                                          */
  261.   opl3_command (ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);    /*
  262.                                  * Reset the
  263.                                  * IRQ of FM
  264.                                  * * chicp
  265.                                  */
  266.  
  267.   if ((stat2 & 0xE0) != 0xc0)
  268.     {
  269.       return 0;            /*
  270.                  * There is no YM3812
  271.                  */
  272.     }
  273.  
  274.   /*
  275.    * There is a FM chicp in this address. Now set some default values.
  276.    */
  277.  
  278.   for (i = 0; i < 9; i++)
  279.     opl3_command (ioaddr, KEYON_BLOCK + i, 0);    /*
  280.                          * Note off
  281.                          */
  282.  
  283.   opl3_command (ioaddr, TEST_REGISTER, ENABLE_WAVE_SELECT);
  284.   opl3_command (ioaddr, PERCUSSION_REGISTER, 0x00);    /*
  285.                              * Melodic mode.
  286.                              */
  287.  
  288.   return 1;
  289. }
  290.  
  291. static int
  292. opl3_kill_note (int dev, int voice, int note, int velocity)
  293. {
  294.   struct physical_voice_info *map;
  295.  
  296.   if (voice < 0 || voice >= nr_voices)
  297.     return 0;
  298.  
  299.   voice_alloc->map[voice] = 0;
  300.  
  301.   map = &physical_voices[logical_voices[voice]];
  302.  
  303.   DEB (printk ("Kill note %d\n", voice));
  304.  
  305.   if (map->voice_mode == 0)
  306.     return 0;
  307.  
  308.   opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, voices[voice].keyon_byte & ~0x20);
  309.  
  310.   voices[voice].keyon_byte = 0;
  311.   voices[voice].bender = 0;
  312.   voices[voice].bender_range = 200;    /*
  313.                      * 200 cents = 2 semitones
  314.                      */
  315.   voices[voice].orig_freq = 0;
  316.   voices[voice].current_freq = 0;
  317.   voices[voice].mode = 0;
  318.  
  319.   return 0;
  320. }
  321.  
  322. #define HIHAT            0
  323. #define CYMBAL            1
  324. #define TOMTOM            2
  325. #define SNARE            3
  326. #define BDRUM            4
  327. #define UNDEFINED        TOMTOM
  328. #define DEFAULT            TOMTOM
  329.  
  330. static int
  331. store_instr (int instr_no, struct sbi_instrument *instr)
  332. {
  333.  
  334.   if (instr->key != FM_PATCH && (instr->key != OPL3_PATCH || !opl3_enabled))
  335.     printk ("FM warning: Invalid patch format field (key) 0x%x\n", instr->key);
  336.   memcpy ((char *) &(instrmap[instr_no]), (char *) instr, sizeof (*instr));
  337.  
  338.   return 0;
  339. }
  340.  
  341. static int
  342. opl3_set_instr (int dev, int voice, int instr_no)
  343. {
  344.   if (voice < 0 || voice >= nr_voices)
  345.     return 0;
  346.  
  347.   if (instr_no < 0 || instr_no >= SBFM_MAXINSTR)
  348.     return 0;
  349.  
  350.   active_instrument[voice] = &instrmap[instr_no];
  351.   return 0;
  352. }
  353.  
  354. /*
  355.  * The next table looks magical, but it certainly is not. Its values have
  356.  * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
  357.  * for i=0. This log-table converts a linear volume-scaling (0..127) to a
  358.  * logarithmic scaling as present in the FM-synthesizer chips. so :    Volume
  359.  * 64 =  0 db = relative volume  0 and:    Volume 32 = -6 db = relative
  360.  * volume -8 it was implemented as a table because it is only 128 bytes and
  361.  * it saves a lot of log() calculations. (RH)
  362.  */
  363. char            fm_volume_table[128] =
  364. {-64, -48, -40, -35, -32, -29, -27, -26,    /*
  365.                          * 0 -   7
  366.                          */
  367.  -24, -23, -21, -20, -19, -18, -18, -17,    /*
  368.                          * 8 -  15
  369.                          */
  370.  -16, -15, -15, -14, -13, -13, -12, -12,    /*
  371.                          * 16 -  23
  372.                          */
  373.  -11, -11, -10, -10, -10, -9, -9, -8,    /*
  374.                      * 24 -  31
  375.                      */
  376.  -8, -8, -7, -7, -7, -6, -6, -6,/*
  377.                      * 32 -  39
  378.                      */
  379.  -5, -5, -5, -5, -4, -4, -4, -4,/*
  380.                      * 40 -  47
  381.                      */
  382.  -3, -3, -3, -3, -2, -2, -2, -2,/*
  383.                      * 48 -  55
  384.                      */
  385.  -2, -1, -1, -1, -1, 0, 0, 0,    /*
  386.                  * 56 -  63
  387.                  */
  388.  0, 0, 0, 1, 1, 1, 1, 1,    /*
  389.                  * 64 -  71
  390.                  */
  391.  1, 2, 2, 2, 2, 2, 2, 2,    /*
  392.                  * 72 -  79
  393.                  */
  394.  3, 3, 3, 3, 3, 3, 3, 4,    /*
  395.                  * 80 -  87
  396.                  */
  397.  4, 4, 4, 4, 4, 4, 4, 5,    /*
  398.                  * 88 -  95
  399.                  */
  400.  5, 5, 5, 5, 5, 5, 5, 5,    /*
  401.                  * 96 - 103
  402.                  */
  403.  6, 6, 6, 6, 6, 6, 6, 6,    /*
  404.                  * 104 - 111
  405.                  */
  406.  6, 7, 7, 7, 7, 7, 7, 7,    /*
  407.                  * 112 - 119
  408.                  */
  409.  7, 7, 7, 8, 8, 8, 8, 8};    /*
  410.  
  411.  
  412.                  * *  * * 120 - 127   */
  413.  
  414. static void
  415. calc_vol (unsigned char *regbyte, int volume)
  416. {
  417.   int             level = (~*regbyte & 0x3f);
  418.  
  419.   if (level)
  420.     level += fm_volume_table[volume];
  421.  
  422.   if (level > 0x3f)
  423.     level = 0x3f;
  424.   if (level < 0)
  425.     level = 0;
  426.  
  427.   *regbyte = (*regbyte & 0xc0) | (~level & 0x3f);
  428. }
  429.  
  430. static void
  431. set_voice_volume (int voice, int volume)
  432. {
  433.   unsigned char   vol1, vol2, vol3, vol4;
  434.   struct sbi_instrument *instr;
  435.   struct physical_voice_info *map;
  436.  
  437.   if (voice < 0 || voice >= nr_voices)
  438.     return;
  439.  
  440.   map = &physical_voices[logical_voices[voice]];
  441.  
  442.   instr = active_instrument[voice];
  443.  
  444.   if (!instr)
  445.     instr = &instrmap[0];
  446.  
  447.   if (instr->channel < 0)
  448.     return;
  449.  
  450.   if (voices[voice].mode == 0)
  451.     return;
  452.  
  453.   if (voices[voice].mode == 2)
  454.     {                /*
  455.                  * 2 OP voice
  456.                  */
  457.  
  458.       vol1 = instr->operators[2];
  459.       vol2 = instr->operators[3];
  460.  
  461.       if ((instr->operators[10] & 0x01))
  462.     {            /*
  463.                  * Additive synthesis
  464.                  */
  465.       calc_vol (&vol1, volume);
  466.       calc_vol (&vol2, volume);
  467.     }
  468.       else
  469.     {            /*
  470.                  * FM synthesis
  471.                  */
  472.       calc_vol (&vol2, volume);
  473.     }
  474.  
  475.       opl3_command (map->ioaddr, KSL_LEVEL + map->op[0], vol1);    /*
  476.                                      * Modulator
  477.                                      * volume
  478.                                      */
  479.       opl3_command (map->ioaddr, KSL_LEVEL + map->op[1], vol2);    /*
  480.                                      * Carrier
  481.                                      * volume
  482.                                      */
  483.     }
  484.   else
  485.     {                /*
  486.                  * 4 OP voice
  487.                  */
  488.       int             connection;
  489.  
  490.       vol1 = instr->operators[2];
  491.       vol2 = instr->operators[3];
  492.       vol3 = instr->operators[OFFS_4OP + 2];
  493.       vol4 = instr->operators[OFFS_4OP + 3];
  494.  
  495.       /*
  496.        * The connection method for 4 OP voices is defined by the rightmost
  497.        * bits at the offsets 10 and 10+OFFS_4OP
  498.        */
  499.  
  500.       connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
  501.  
  502.       switch (connection)
  503.     {
  504.     case 0:
  505.       calc_vol (&vol4, volume);    /*
  506.                      * Just the OP 4 is carrier
  507.                      */
  508.       break;
  509.  
  510.     case 1:
  511.       calc_vol (&vol2, volume);
  512.       calc_vol (&vol4, volume);
  513.       break;
  514.  
  515.     case 2:
  516.       calc_vol (&vol1, volume);
  517.       calc_vol (&vol4, volume);
  518.       break;
  519.  
  520.     case 3:
  521.       calc_vol (&vol1, volume);
  522.       calc_vol (&vol3, volume);
  523.       calc_vol (&vol4, volume);
  524.       break;
  525.  
  526.     default:        /*
  527.                  * Why ??
  528.                                                                                                                  */ ;
  529.     }
  530.  
  531.       opl3_command (map->ioaddr, KSL_LEVEL + map->op[0], vol1);
  532.       opl3_command (map->ioaddr, KSL_LEVEL + map->op[1], vol2);
  533.       opl3_command (map->ioaddr, KSL_LEVEL + map->op[2], vol3);
  534.       opl3_command (map->ioaddr, KSL_LEVEL + map->op[3], vol4);
  535.     }
  536. }
  537.  
  538. static int
  539. opl3_start_note (int dev, int voice, int note, int volume)
  540. {
  541.   unsigned char   data, fpc;
  542.   int             block, fnum, freq, voice_mode;
  543.   struct sbi_instrument *instr;
  544.   struct physical_voice_info *map;
  545.  
  546.   if (voice < 0 || voice >= nr_voices)
  547.     return 0;
  548.  
  549.   map = &physical_voices[logical_voices[voice]];
  550.  
  551.   if (map->voice_mode == 0)
  552.     return 0;
  553.  
  554.   if (note == 255)        /*
  555.                  * Just change the volume
  556.                  */
  557.     {
  558.       set_voice_volume (voice, volume);
  559.       return 0;
  560.     }
  561.  
  562.   /*
  563.    * Kill previous note before playing
  564.    */
  565.   opl3_command (map->ioaddr, KSL_LEVEL + map->op[1], 0xff);    /*
  566.                                  * Carrier
  567.                                  * volume to
  568.                                  * min
  569.                                  */
  570.   opl3_command (map->ioaddr, KSL_LEVEL + map->op[0], 0xff);    /*
  571.                                  * Modulator
  572.                                  * volume to
  573.                                  */
  574.  
  575.   if (map->voice_mode == 4)
  576.     {
  577.       opl3_command (map->ioaddr, KSL_LEVEL + map->op[2], 0xff);
  578.       opl3_command (map->ioaddr, KSL_LEVEL + map->op[3], 0xff);
  579.     }
  580.  
  581.   opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, 0x00);    /*
  582.                                      * Note
  583.                                      * off
  584.                                      */
  585.  
  586.   instr = active_instrument[voice];
  587.  
  588.   if (!instr)
  589.     instr = &instrmap[0];
  590.  
  591.   if (instr->channel < 0)
  592.     {
  593.       printk (
  594.            "OPL3: Initializing voice %d with undefined instrument\n",
  595.            voice);
  596.       return 0;
  597.     }
  598.  
  599.   if (map->voice_mode == 2 && instr->key == OPL3_PATCH)
  600.     return 0;            /*
  601.                  * Cannot play
  602.                  */
  603.  
  604.   voice_mode = map->voice_mode;
  605.  
  606.   if (voice_mode == 4)
  607.     {
  608.       int             voice_shift;
  609.  
  610.       voice_shift = (map->ioaddr == left_address) ? 0 : 3;
  611.       voice_shift += map->voice_num;
  612.  
  613.       if (instr->key != OPL3_PATCH)    /*
  614.                      * Just 2 OP patch
  615.                      */
  616.     {
  617.       voice_mode = 2;
  618.       connection_mask &= ~(1 << voice_shift);
  619.     }
  620.       else
  621.     {
  622.       connection_mask |= (1 << voice_shift);
  623.     }
  624.  
  625.       opl3_command (right_address, CONNECTION_SELECT_REGISTER, connection_mask);
  626.     }
  627.  
  628.   /*
  629.    * Set Sound Characteristics
  630.    */
  631.   opl3_command (map->ioaddr, AM_VIB + map->op[0], instr->operators[0]);
  632.   opl3_command (map->ioaddr, AM_VIB + map->op[1], instr->operators[1]);
  633.  
  634.   /*
  635.    * Set Attack/Decay
  636.    */
  637.   opl3_command (map->ioaddr, ATTACK_DECAY + map->op[0], instr->operators[4]);
  638.   opl3_command (map->ioaddr, ATTACK_DECAY + map->op[1], instr->operators[5]);
  639.  
  640.   /*
  641.    * Set Sustain/Release
  642.    */
  643.   opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[0], instr->operators[6]);
  644.   opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[1], instr->operators[7]);
  645.  
  646.   /*
  647.    * Set Wave Select
  648.    */
  649.   opl3_command (map->ioaddr, WAVE_SELECT + map->op[0], instr->operators[8]);
  650.   opl3_command (map->ioaddr, WAVE_SELECT + map->op[1], instr->operators[9]);
  651.  
  652.   /*
  653.    * Set Feedback/Connection
  654.    */
  655.   fpc = instr->operators[10];
  656.   if (!(fpc & 0x30))
  657.     fpc |= 0x30;        /*
  658.                  * Ensure that at least one chn is enabled
  659.                  */
  660.   opl3_command (map->ioaddr, FEEDBACK_CONNECTION + map->voice_num,
  661.         fpc);
  662.  
  663.   /*
  664.    * If the voice is a 4 OP one, initialize the operators 3 and 4 also
  665.    */
  666.  
  667.   if (voice_mode == 4)
  668.     {
  669.  
  670.       /*
  671.        * Set Sound Characteristics
  672.        */
  673.       opl3_command (map->ioaddr, AM_VIB + map->op[2], instr->operators[OFFS_4OP + 0]);
  674.       opl3_command (map->ioaddr, AM_VIB + map->op[3], instr->operators[OFFS_4OP + 1]);
  675.  
  676.       /*
  677.        * Set Attack/Decay
  678.        */
  679.       opl3_command (map->ioaddr, ATTACK_DECAY + map->op[2], instr->operators[OFFS_4OP + 4]);
  680.       opl3_command (map->ioaddr, ATTACK_DECAY + map->op[3], instr->operators[OFFS_4OP + 5]);
  681.  
  682.       /*
  683.        * Set Sustain/Release
  684.        */
  685.       opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[2], instr->operators[OFFS_4OP + 6]);
  686.       opl3_command (map->ioaddr, SUSTAIN_RELEASE + map->op[3], instr->operators[OFFS_4OP + 7]);
  687.  
  688.       /*
  689.        * Set Wave Select
  690.        */
  691.       opl3_command (map->ioaddr, WAVE_SELECT + map->op[2], instr->operators[OFFS_4OP + 8]);
  692.       opl3_command (map->ioaddr, WAVE_SELECT + map->op[3], instr->operators[OFFS_4OP + 9]);
  693.  
  694.       /*
  695.        * Set Feedback/Connection
  696.        */
  697.       fpc = instr->operators[OFFS_4OP + 10];
  698.       if (!(fpc & 0x30))
  699.     fpc |= 0x30;        /*
  700.                  * Ensure that at least one chn is enabled
  701.                  */
  702.       opl3_command (map->ioaddr, FEEDBACK_CONNECTION + map->voice_num + 3, fpc);
  703.     }
  704.  
  705.   voices[voice].mode = voice_mode;
  706.  
  707.   set_voice_volume (voice, volume);
  708.  
  709.   freq = voices[voice].orig_freq = note_to_freq (note) / 1000;
  710.  
  711.   /*
  712.    * Since the pitch bender may have been set before playing the note, we
  713.    * have to calculate the bending now.
  714.    */
  715.  
  716.   freq = compute_finetune (voices[voice].orig_freq, voices[voice].bender, voices[voice].bender_range);
  717.   voices[voice].current_freq = freq;
  718.  
  719.   freq_to_fnum (freq, &block, &fnum);
  720.  
  721.   /*
  722.    * Play note
  723.    */
  724.  
  725.   data = fnum & 0xff;        /*
  726.                  * Least significant bits of fnumber
  727.                  */
  728.   opl3_command (map->ioaddr, FNUM_LOW + map->voice_num, data);
  729.  
  730.   data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);
  731.   voices[voice].keyon_byte = data;
  732.   opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, data);
  733.   if (voice_mode == 4)
  734.     opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num + 3, data);
  735.  
  736.   return 0;
  737. }
  738.  
  739. static void
  740. freq_to_fnum (int freq, int *block, int *fnum)
  741. {
  742.   int             f, octave;
  743.  
  744.   /*
  745.    * Converts the note frequency to block and fnum values for the FM chip
  746.    */
  747.   /*
  748.    * First try to compute the block -value (octave) where the note belongs
  749.    */
  750.  
  751.   f = freq;
  752.  
  753.   octave = 5;
  754.  
  755.   if (f == 0)
  756.     octave = 0;
  757.   else if (f < 261)
  758.     {
  759.       while (f < 261)
  760.     {
  761.       octave--;
  762.       f <<= 1;
  763.     }
  764.     }
  765.   else if (f > 493)
  766.     {
  767.       while (f > 493)
  768.     {
  769.       octave++;
  770.       f >>= 1;
  771.     }
  772.     }
  773.  
  774.   if (octave > 7)
  775.     octave = 7;
  776.  
  777.   *fnum = freq * (1 << (20 - octave)) / 49716;
  778.   *block = octave;
  779. }
  780.  
  781. static void
  782. opl3_command (int io_addr, unsigned int addr, unsigned int val)
  783. {
  784.   int             i;
  785.  
  786.   /*
  787.    * The original 2-OP synth requires a quite long delay after writing to a
  788.    * register. The OPL-3 survives with just two INBs
  789.    */
  790.  
  791.   OUTB ((unsigned char) (addr & 0xff), io_addr);    /*
  792.                              * Select register
  793.                              *
  794.                              */
  795.  
  796.   if (!opl3_enabled)
  797.     tenmicrosec ();
  798.   else
  799.     for (i = 0; i < 2; i++)
  800.       INB (io_addr);
  801.  
  802.   OUTB ((unsigned char) (val & 0xff), io_addr + 1);    /*
  803.                              * Write to register
  804.                              *
  805.                              */
  806.  
  807.   if (!opl3_enabled)
  808.     {
  809.       tenmicrosec ();
  810.       tenmicrosec ();
  811.       tenmicrosec ();
  812.     }
  813.   else
  814.     for (i = 0; i < 2; i++)
  815.       INB (io_addr);
  816. }
  817.  
  818. static void
  819. opl3_reset (int dev)
  820. {
  821.   int             i;
  822.  
  823.   for (i = 0; i < nr_voices; i++)
  824.     {
  825.       opl3_command (physical_voices[logical_voices[i]].ioaddr,
  826.         KSL_LEVEL + physical_voices[logical_voices[i]].op[0], 0xff);
  827.  
  828.       opl3_command (physical_voices[logical_voices[i]].ioaddr,
  829.         KSL_LEVEL + physical_voices[logical_voices[i]].op[1], 0xff);
  830.  
  831.       if (physical_voices[logical_voices[i]].voice_mode == 4)
  832.     {
  833.       opl3_command (physical_voices[logical_voices[i]].ioaddr,
  834.         KSL_LEVEL + physical_voices[logical_voices[i]].op[2], 0xff);
  835.  
  836.       opl3_command (physical_voices[logical_voices[i]].ioaddr,
  837.         KSL_LEVEL + physical_voices[logical_voices[i]].op[3], 0xff);
  838.     }
  839.  
  840.       opl3_kill_note (dev, i, 0, 64);
  841.     }
  842.  
  843.   if (opl3_enabled)
  844.     {
  845.       voice_alloc->max_voice = nr_voices = 18;
  846.  
  847.       for (i = 0; i < 18; i++)
  848.     logical_voices[i] = i;
  849.  
  850.       for (i = 0; i < 18; i++)
  851.     physical_voices[i].voice_mode = 2;
  852.  
  853.     }
  854.  
  855. }
  856.  
  857. static int
  858. opl3_open (int dev, int mode)
  859. {
  860.   if (!opl3_ok)
  861.     return RET_ERROR (ENXIO);
  862.   if (opl3_busy)
  863.     return RET_ERROR (EBUSY);
  864.   opl3_busy = 1;
  865.  
  866.   connection_mask = 0x00;    /*
  867.                  * Just 2 OP voices
  868.                  */
  869.   if (opl3_enabled)
  870.     opl3_command (right_address, CONNECTION_SELECT_REGISTER, connection_mask);
  871.   return 0;
  872. }
  873.  
  874. static void
  875. opl3_close (int dev)
  876. {
  877.   opl3_busy = 0;
  878.   voice_alloc->max_voice = nr_voices = opl3_enabled ? 18 : 9;
  879.   fm_info.nr_drums = 0;
  880.   fm_info.perc_mode = 0;
  881.  
  882.   opl3_reset (dev);
  883. }
  884.  
  885. static void
  886. opl3_hw_control (int dev, unsigned char *event)
  887. {
  888. }
  889.  
  890. static int
  891. opl3_load_patch (int dev, int format, snd_rw_buf * addr,
  892.          int offs, int count, int pmgr_flag)
  893. {
  894.   struct sbi_instrument ins;
  895.  
  896.   if (count < sizeof (ins))
  897.     {
  898.       printk ("FM Error: Patch record too short\n");
  899.       return RET_ERROR (EINVAL);
  900.     }
  901.  
  902.   COPY_FROM_USER (&((char *) &ins)[offs], (char *) addr, offs, sizeof (ins) - offs);
  903.  
  904.   if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)
  905.     {
  906.       printk ("FM Error: Invalid instrument number %d\n", ins.channel);
  907.       return RET_ERROR (EINVAL);
  908.     }
  909.   ins.key = format;
  910.  
  911.   return store_instr (ins.channel, &ins);
  912. }
  913.  
  914. static void
  915. opl3_panning (int dev, int voice, int pressure)
  916. {
  917. }
  918.  
  919. static void
  920. opl3_volume_method (int dev, int mode)
  921. {
  922. }
  923.  
  924. #define SET_VIBRATO(cell) { \
  925.       tmp = instr->operators[(cell-1)+(((cell-1)/2)*OFFS_4OP)]; \
  926.       if (pressure > 110) \
  927.     tmp |= 0x40;        /* Vibrato on */ \
  928.       opl3_command (map->ioaddr, AM_VIB + map->op[cell-1], tmp);}
  929.  
  930. static void
  931. opl3_aftertouch (int dev, int voice, int pressure)
  932. {
  933.   int             tmp;
  934.   struct sbi_instrument *instr;
  935.   struct physical_voice_info *map;
  936.  
  937.   if (voice < 0 || voice >= nr_voices)
  938.     return;
  939.  
  940.   map = &physical_voices[logical_voices[voice]];
  941.  
  942.   DEB (printk ("Aftertouch %d\n", voice));
  943.  
  944.   if (map->voice_mode == 0)
  945.     return;
  946.  
  947.   /*
  948.    * Adjust the amount of vibrato depending the pressure
  949.    */
  950.  
  951.   instr = active_instrument[voice];
  952.  
  953.   if (!instr)
  954.     instr = &instrmap[0];
  955.  
  956.   if (voices[voice].mode == 4)
  957.     {
  958.       int             connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
  959.  
  960.       switch (connection)
  961.     {
  962.     case 0:
  963.       SET_VIBRATO (4);
  964.       break;
  965.  
  966.     case 1:
  967.       SET_VIBRATO (2);
  968.       SET_VIBRATO (4);
  969.       break;
  970.  
  971.     case 2:
  972.       SET_VIBRATO (1);
  973.       SET_VIBRATO (4);
  974.       break;
  975.  
  976.     case 3:
  977.       SET_VIBRATO (1);
  978.       SET_VIBRATO (3);
  979.       SET_VIBRATO (4);
  980.       break;
  981.  
  982.     }
  983.       /*
  984.        * Not implemented yet
  985.        */
  986.     }
  987.   else
  988.     {
  989.       SET_VIBRATO (1);
  990.  
  991.       if ((instr->operators[10] & 0x01))    /*
  992.                          * Additive synthesis
  993.                          */
  994.     SET_VIBRATO (2);
  995.     }
  996. }
  997.  
  998. #undef SET_VIBRATO
  999.  
  1000. static void
  1001. bend_pitch (int dev, int voice, int value)
  1002. {
  1003.   unsigned char   data;
  1004.   int             block, fnum, freq;
  1005.   struct physical_voice_info *map;
  1006.  
  1007.   map = &physical_voices[logical_voices[voice]];
  1008.  
  1009.   if (map->voice_mode == 0)
  1010.     return;
  1011.  
  1012.   voices[voice].bender = value;
  1013.   if (!value)
  1014.     return;
  1015.   if (!(voices[voice].keyon_byte & 0x20))
  1016.     return;            /*
  1017.                  * Not keyed on
  1018.                  */
  1019.  
  1020.   freq = compute_finetune (voices[voice].orig_freq, voices[voice].bender, voices[voice].bender_range);
  1021.   voices[voice].current_freq = freq;
  1022.  
  1023.   freq_to_fnum (freq, &block, &fnum);
  1024.  
  1025.   data = fnum & 0xff;        /*
  1026.                  * Least significant bits of fnumber
  1027.                  */
  1028.   opl3_command (map->ioaddr, FNUM_LOW + map->voice_num, data);
  1029.  
  1030.   data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3);    /*
  1031.                                  * *
  1032.                                  * KEYON|OCTAVE|MS
  1033.                                  *
  1034.                                  * * bits * *
  1035.                                  * of * f-num
  1036.                                  *
  1037.                                  */
  1038.   voices[voice].keyon_byte = data;
  1039.   opl3_command (map->ioaddr, KEYON_BLOCK + map->voice_num, data);
  1040. }
  1041.  
  1042. static void
  1043. opl3_controller (int dev, int voice, int ctrl_num, int value)
  1044. {
  1045.   if (voice < 0 || voice >= nr_voices)
  1046.     return;
  1047.  
  1048.   switch (ctrl_num)
  1049.     {
  1050.     case CTRL_PITCH_BENDER:
  1051.       bend_pitch (dev, voice, value);
  1052.       break;
  1053.  
  1054.     case CTRL_PITCH_BENDER_RANGE:
  1055.       voices[voice].bender_range = value;
  1056.       break;
  1057.     }
  1058. }
  1059.  
  1060. static int
  1061. opl3_patchmgr (int dev, struct patmgr_info *rec)
  1062. {
  1063.   return RET_ERROR (EINVAL);
  1064. }
  1065.  
  1066. static void
  1067. opl3_bender (int dev, int voice, int value)
  1068. {
  1069.   if (voice < 0 || voice >= nr_voices)
  1070.     return;
  1071.  
  1072.   bend_pitch (dev, voice, value);
  1073. }
  1074.  
  1075. static int
  1076. opl3_alloc_voice (int dev, int chn, int note, struct voice_alloc_info *alloc)
  1077. {
  1078.   int             i, p, avail_voices;
  1079.   struct sbi_instrument *instr;
  1080.   int             is4op;
  1081.   int             instr_no;
  1082.  
  1083.   if (chn < 0 || chn > 15)
  1084.     instr_no = 0;
  1085.   else
  1086.     instr_no = chn_info[chn].pgm_num;
  1087.  
  1088.   instr = &instrmap[instr_no];
  1089.   if (instr->channel < 0 ||    /* Instrument not loaded */
  1090.       nr_voices != 12)        /* Not in 4 OP mode */
  1091.     is4op = 0;
  1092.   else if (nr_voices == 12)    /* 4 OP mode */
  1093.     is4op = (instr->key == OPL3_PATCH);
  1094.   else
  1095.     is4op = 0;
  1096.  
  1097.   if (is4op)
  1098.     {
  1099.       p = 0;
  1100.       avail_voices = 6;
  1101.     }
  1102.   else
  1103.     {
  1104.       if (nr_voices == 12)    /* 4 OP mode. Use the '2 OP only' voices first */
  1105.     p = 6;
  1106.       else
  1107.     p = 0;
  1108.       avail_voices = nr_voices;
  1109.     }
  1110.  
  1111.   /*
  1112.  *    Now try to find a free voice
  1113.  */
  1114.  
  1115.   for (i = 0; i < avail_voices; i++)
  1116.     {
  1117.       if (alloc->map[p] == 0)
  1118.     {
  1119.       return p;
  1120.     }
  1121.       p = (p + 1) % nr_voices;
  1122.     }
  1123.  
  1124.   /*
  1125.  *    Insert some kind of priority mechanism here.
  1126.  */
  1127.  
  1128.   printk ("OPL3: Out of free voices\n");
  1129.   return 0;            /* All voices in use. Select the first one. */
  1130. }
  1131.  
  1132. static struct synth_operations opl3_operations =
  1133. {
  1134.   &fm_info,
  1135.   0,
  1136.   SYNTH_TYPE_FM,
  1137.   FM_TYPE_ADLIB,
  1138.   opl3_open,
  1139.   opl3_close,
  1140.   opl3_ioctl,
  1141.   opl3_kill_note,
  1142.   opl3_start_note,
  1143.   opl3_set_instr,
  1144.   opl3_reset,
  1145.   opl3_hw_control,
  1146.   opl3_load_patch,
  1147.   opl3_aftertouch,
  1148.   opl3_controller,
  1149.   opl3_panning,
  1150.   opl3_volume_method,
  1151.   opl3_patchmgr,
  1152.   opl3_bender,
  1153.   opl3_alloc_voice
  1154. };
  1155.  
  1156. long
  1157. opl3_init (long mem_start)
  1158. {
  1159.   int             i;
  1160.  
  1161.   PERMANENT_MALLOC (struct sbi_instrument *, instrmap,
  1162.             SBFM_MAXINSTR * sizeof (*instrmap), mem_start);
  1163.  
  1164.   if (num_synths >= MAX_SYNTH_DEV)
  1165.     printk ("OPL3 Error: Too many synthesizers\n");
  1166.   else
  1167.     {
  1168.       synth_devs[num_synths++] = &opl3_operations;
  1169.       voice_alloc = &opl3_operations.alloc;
  1170.       chn_info = &opl3_operations.chn_info[0];
  1171.     }
  1172.  
  1173.   fm_model = 0;
  1174.   opl3_ok = 1;
  1175.   if (opl3_enabled)
  1176.     {
  1177. #ifdef __FreeBSD__
  1178.       printk ("snd1: <Yamaha OPL-3 FM>");
  1179. #else
  1180.       printk (" <Yamaha OPL-3 FM>");
  1181. #endif
  1182.       fm_model = 2;
  1183.       voice_alloc->max_voice = nr_voices = 18;
  1184.       fm_info.nr_drums = 0;
  1185.       fm_info.capabilities |= SYNTH_CAP_OPL3;
  1186. #ifndef SCO
  1187.       strcpy (fm_info.name, "Yamaha OPL-3");
  1188. #endif
  1189.  
  1190.       for (i = 0; i < 18; i++)
  1191.     if (physical_voices[i].ioaddr == USE_LEFT)
  1192.       physical_voices[i].ioaddr = left_address;
  1193.     else
  1194.       physical_voices[i].ioaddr = right_address;
  1195.  
  1196.       /* Enable OPL-3 mode */
  1197.       opl3_command (right_address, OPL3_MODE_REGISTER, OPL3_ENABLE);
  1198.  
  1199.       /* Select all 2-OP voices */
  1200.       opl3_command (right_address, CONNECTION_SELECT_REGISTER, 0x00);
  1201.     }
  1202.   else
  1203.     {
  1204. #ifdef __FreeBSD__
  1205.       printk ("snd1: <Yamaha 2-OP FM>");
  1206. #else
  1207.       printk (" <Yamaha 2-OP FM>");
  1208. #endif
  1209.       fm_model = 1;
  1210.       voice_alloc->max_voice = nr_voices = 9;
  1211.       fm_info.nr_drums = 0;
  1212.  
  1213.       for (i = 0; i < 18; i++)
  1214.     physical_voices[i].ioaddr = left_address;
  1215.     };
  1216.  
  1217.   already_initialized = 1;
  1218.   for (i = 0; i < SBFM_MAXINSTR; i++)
  1219.     instrmap[i].channel = -1;
  1220.  
  1221.   return mem_start;
  1222. }
  1223.  
  1224. #endif
  1225.